package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.entity.ExpPayReqDetail;
import com.softer.wxzj.entity.OwnerSignature;
import com.softer.wxzj.mapper.ExpPayReqDetailMapper;
import com.softer.wxzj.service.ExpPayReqDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ExpPayReqDetailServiceImpl implements ExpPayReqDetailService {
    @Autowired
    private ExpPayReqDetailMapper expPayReqDetailMapper;

    @Override
    public ExpPayReqDetail getSumByReqId(String reqId) {
        return null;
    }

    @Override
    public PageInfo<ExpPayReqDetail> selectByPayReqId(String id, Page page) {
        //总计
        ExpPayReqDetail totalSum = expPayReqDetailMapper.selectByPayReqIdSum(id);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<ExpPayReqDetail> expPayReqDetailList = expPayReqDetailMapper.selectByPayReqId(id);
        BigDecimal allsa = new BigDecimal(0.00);
        BigDecimal allfbaab = new BigDecimal(0.00);
        BigDecimal allaa = new BigDecimal(0.00);
        BigDecimal allg = new BigDecimal(0.00);
        BigDecimal allad = new BigDecimal(0.00);
        //currentSum当前页总计
        ExpPayReqDetail currentSum = new ExpPayReqDetail();
        for (ExpPayReqDetail e : expPayReqDetailList) {
            OwnerSignature ownerSignature = expPayReqDetailMapper.getOwnerSignatureInfo(e.getReqId(),e.getHouseId());
            e.setOwnerName(ownerSignature.getOwnerName());
            e.setHouseSite(ownerSignature.getHouseSite());
            allsa = allsa.add(e.getStruArea());
            allfbaab = allfbaab.add(e.getFundBalAndAccrBal());
            allaa = allaa.add(e.getPayAmount());
            allg = allg.add(e.getMoney());
            allad = allad.add(e.getSelfAmount());
        }
        currentSum.setStruArea(allsa);
        currentSum.setFundBalAndAccrBal(allfbaab);
        currentSum.setPayAmount(allaa);
        currentSum.setMoney(allg);
        currentSum.setSelfAmount(allad);

        expPayReqDetailList.add(currentSum);
        expPayReqDetailList.add(totalSum);
        PageInfo<ExpPayReqDetail> info = new PageInfo<>(expPayReqDetailList);
        return info;
    }
}
